﻿/*
 * Author: Pham Danh Vu
 * Solution Confict: Drity Read
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using DGVExample;

namespace DGVExample
{
    public partial class frMainConfict : Form
    {
        public frMainConfict()
        {
            InitializeComponent();
        }

        /*
        * To do Find MaVe
        * show data from Database (QLRAPPHIM) to DataGridView HoaDon
        */
        private void btmMaHD_Click(object sender, EventArgs e)
        {
            string mave = txtbTimHoaDon.Text;
        
            ConnectDatabase Connect_DB = new ConnectDatabase();
            Connect_DB.ConnectDB();

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connect_DB.conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_Search_Ve";

            SqlParameter p1 = new SqlParameter("@mave", mave);
            cmd.Parameters.Add(p1);
            try
            {            
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable tb = new DataTable();

                da.Fill(tb);
                dgvHoaDon.DataSource = tb;
                Connect_DB.conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                Connect_DB.conn.Close();
            }          
        }
        /*
         * To do Find all Ve
         * show data from Database ( QLRAPPHIM) to DataGridView HoaDon
         */
        private void btmTatCa_Click(object sender, EventArgs e)
        {
            try
            {
                ConnectDatabase Connect_DB = new ConnectDatabase();
                Connect_DB.ConnectDB();

                SqlCommand cm = new SqlCommand("sp_select_all_Ve_Saled", Connect_DB.conn);
                cm.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cm);
                DataTable tb = new DataTable();

                da.Fill(tb);
                dgvHoaDon.DataSource = tb;

                Connect_DB.conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /*
         * To do Find Ve-confict
         * show data from Database ( QLRAPPHIM) to DataGridView DanhMucVe
         */
        private void btmSearchPhim_Click(object sender, EventArgs e)
        {
            try
            {
                ConnectDatabase Connect_DB = new ConnectDatabase();
                Connect_DB.ConnectDB();

                SqlCommand cm = new SqlCommand("sp_select_ve_confict", Connect_DB.conn);
                cm.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cm);
                DataTable tb = new DataTable();

                da.Fill(tb);
                dgvDanhMucVe.DataSource = tb;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /*
         * To do Find Ve-Unconfict
         * show data from Database ( QLRAPPHIM) to DataGridView DanhMucVe
         */
        private void btmUnconfict_Click(object sender, EventArgs e)
        {
            try
            {
                ConnectDatabase Connect_DB = new ConnectDatabase();
                Connect_DB.ConnectDB();

                SqlCommand cm = new SqlCommand("sp_select_ve_unconfict", Connect_DB.conn);
                cm.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cm);
                DataTable tb = new DataTable();

                da.Fill(tb);
                dgvDanhMucVe.DataSource = tb;
                
                Connect_DB.conn.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
       
        /*
         * to do delete Ve
         */
        private void btmXoa_Click(object sender, EventArgs e)
        {
            
            string mave = txtbTimHoaDon.Text;

            ConnectDatabase Connect_DB = new ConnectDatabase();
            Connect_DB.ConnectDB();

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connect_DB.conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_delete_hoadon";
            SqlParameter p1 = new SqlParameter("@mave", mave);
            cmd.Parameters.Add(p1);
             try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("Delete has Rollback");
                Connect_DB.conn.Close();
            }
            catch (Exception ex)
        	{
		        MessageBox.Show(ex.ToString());
                Connect_DB.conn.Close();
		    }          
        }
    }
}
